<!--
(C) Copyright 2015 Nuxeo SA (http://nuxeo.com/) and contributors.
All rights reserved. This program and the accompanying materials
are made available under the terms of the GNU Lesser General Public License
(LGPL) version 2.1 which accompanies this distribution, and is available at
http://www.gnu.org/licenses/lgpl.html
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
Contributors:
  Nelson Silva <nsilva@nuxeo.com>
-->
<link rel="import" href="../nuxeo-chart-data-behavior/nuxeo-chart-data-behavior.html">
<link rel="import" href="../nuxeo-data-table/nuxeo-data-table.html">
<dom-module id="nuxeo-search-analytics-dashboard">

  <link rel="import" type="css" href="../dashboard.css">

  <template>

    <!-- Number of calls per PageProvider -->
    <nuxeo-search-data start-date="[[startDate]]" end-date="[[endDate]]"
                       grouped-by="pageProviderName"
                       data="{{callsPerProvider}}"
                       index="[[index]]">
    </nuxeo-search-data>

    <paper-card heading="Calls per PageProvider" elevation="2">
      <div class="card-content">
        <chart-pie values="[[_values(callsPerProvider)]]"
                   series="[[_series(callsPerProvider)]]"
                   colors="[[colors]]"
                   options='{"responsive": true, "animation": false}'>
        </chart-pie>
      </div>
    </paper-card>

    <!-- Number of calls per hour -->
    <nuxeo-search-data start-date="[[startDate]]" end-date="[[endDate]]"
                       with-date-intervals="hour"
                       without-extended-bounds
                       date-format="HH"
                       data="{{callsPerHour}}"
                       index="[[index]]">
    </nuxeo-search-data>

    <paper-card heading="Calls per hour" elevation="2">
      <div class="card-content">
        <chart-bar labels="[[_range(0,23)]]"
                   values="[[_aggregatePerHourOfDay(callsPerHour)]]"
                   options='{"responsive": true, "animation": false}'>
        </chart-bar>
      </div>
    </paper-card>

    <!-- Result ranges -->
    <nuxeo-search-data start-date="[[startDate]]" end-date="[[endDate]]"
                       with-ranges='{"resultsCount":[
                     {"key": "no result", "to": 1 },
                     {"key": "less than 50", "from" : 1, "to": 50},
                     {"key": "between 51 and 200", "from" : 51, "to": 200 },
                     {"key": "between 200 and 1000", "from" : 201, "to": 1000},
                     {"key": "more than 1000", "from" : 1001 }]}'
                       data="{{callPerNumberOfResults}}"
                       index="[[index]]">
    </nuxeo-search-data>

    <paper-card heading="Number of results" elevation="2">
      <div class="card-content">
        <nuxeo-data-table data="[[callPerNumberOfResults]]">
        </nuxeo-data-table>
      </div>
    </paper-card>

    <!-- Most used expressions for full text search  -->

    <nuxeo-search-data start-date="[[startDate]]" end-date="[[endDate]]"
                       grouped-by="searchDocumentModel.properties.defaults:ecm_fulltext"
                       group-limit="5"
                       data="{{callsPerFT}}"
                       index="[[index]]">
    </nuxeo-search-data>

    <paper-card heading="Full text searches" elevation="2">
      <div class="card-content">
        <nuxeo-data-table columns='["Search term", "Number of calls"]'
                          data="[[callsPerFT]]">
        </nuxeo-data-table>
      </div>
    </paper-card>


    <!-- Searches by number of pages displayed -->

    <nuxeo-search-data start-date="[[startDate]]" end-date="[[endDate]]"
                       with-ranges='{"pageIndex":[
                         {"key": "First page", "from" : 0, "to": 1 },
                         {"key": "Page 2", "from" : 1, "to": 2},
                         {"key": "Pages 3 to 5", "from" : 2, "to": 5 },
                         {"key": "Pages 6 to 10", "from" : 6, "to": 10},
                         {"key": "After 10 pages", "from" : 10 }]}'
                       data="{{callPerNumberOfPages}}"
                       index="[[index]]">
    </nuxeo-search-data>

    <paper-card heading="Number of pages displayed" elevation="2">
      <div class="card-content">
        <nuxeo-data-table data="[[callPerNumberOfPages]]">
        </nuxeo-data-table>
      </div>
    </paper-card>

    <!-- Searches by filters used -->
    <nuxeo-search-data start-date="[[startDate]]" end-date="[[endDate]]"
                       grouped-by="searchFields"
                       group-limit="5"
                       data="{{callByFilters}}">
    </nuxeo-search-data>

    <paper-card heading="Filters used" elevation="2">
      <div class="card-content">
        <nuxeo-data-table columns='["Filters used", "Number of calls"]'
                          data="[[callByFilters]]">
        </nuxeo-data-table>
      </div>
    </paper-card>

  </template>

</dom-module>

<script>
  Polymer({
    is: 'nuxeo-search-analytics-dashboard',
    behaviors: [Nuxeo.ChartDataBehavior],
    properties: {
      index: {
        type: String,
        value: 'audit'
      },
      startDate: String,
      endDate: String,
      colors: {
        type: Array,
        value: ['#fb8072', '#8dd3c7', '#ffffb3', '#bebada', '#80b1d3', '#fdb462', '#b3de69', '#fccde5', '#d9d9d9', '#bc80bd']
      },
      hoursBounds: {
        value: {min: 0, max: 23}
      }
    },

    _range: function(start, end) {
      var res = [];
      for (var i = start; i <= end; i++) {
        res.push(i);
      }
      return res;
    },

    _aggregatePerHourOfDay: function(entries) {
      // aggregate our buckets by key
      var agg = {};
      entries.forEach(function(e) {
        agg[e.key] = agg[e.key] || [];
        agg[e.key].push(e.value);
      });
      // build our total per bucket
      var hours = this._range(this.hoursBounds.min, this.hoursBounds.max);
      return [hours.map(function(i) {
        if (!agg[i] || !agg[i].length) { return 0; }
        // TODO: use Array.reduce once prototype.js is removed!
        // return agg[i].reduce(function(a, b) { return a + b; });
        var sum = 0;
        agg[i].forEach(function(v) { sum += v; });
        return sum;
      })];
    }
  });
</script>
